/*
The MIT License

Copyright (C) 2008 Yu Kobayashi http://yukoba.accelart.jp/

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package com.logicfishsoftware.wolfgo.mount.dokan;

import static net.decasdev.dokan.FileAttribute.FileAttributeFlags.FILE_ATTRIBUTE_DIRECTORY;
import static net.decasdev.dokan.FileAttribute.FileAttributeFlags.FILE_ATTRIBUTE_NORMAL;

import java.util.Date;

import net.decasdev.dokan.ByHandleFileInformation;
import net.decasdev.dokan.FileTimeUtils;
import net.decasdev.dokan.Win32FindData;

import org.apache.commons.io.FilenameUtils;

import com.logicfishsoftware.wolfgo.api.VirtualDirectory;
import com.logicfishsoftware.wolfgo.api.VirtualFile;

public class FileDevice 
//implements VirtualFile 
{
	private static long nextFileIndex = 2;

	String fileName;
	private final boolean isDirectory;
//	final TByteArrayList content = new TByteArrayList();
	private int fileAttribute = FILE_ATTRIBUTE_NORMAL.getValue();
	private long creationTime = 0;
	private long lastAccessTime = 0;
	private long lastWriteTime = 0;
	private final long fileIndex;

	private int fileSize;

	private VirtualDirectory parent;

	protected FileDevice(String fileName, boolean isDirectory) {
		this.fileName = fileName;
		this.isDirectory = isDirectory;
		fileIndex = getNextFileIndex();
		if (isDirectory) {
			fileAttribute |= FILE_ATTRIBUTE_DIRECTORY.getValue();
		}
		long fileTime = FileTimeUtils.toFileTime(new Date());
		creationTime = fileTime;
		lastAccessTime = fileTime;
		lastWriteTime = fileTime;
	}

	protected FileDevice(VirtualFile fileInfo) {
		this.isDirectory = fileInfo instanceof VirtualDirectory;
		this.fileIndex = fileInfo.getFileIndex();
		this.fileName = fileInfo.getFileName();
		this.fileAttribute = fileInfo.getFileAttribute();
		if(isDirectory()) {
			this.fileAttribute |= FILE_ATTRIBUTE_DIRECTORY.getValue();
		}
	}

	Win32FindData toWin32FindData() {
		return new Win32FindData(fileAttribute, creationTime, lastAccessTime, lastWriteTime,
				getFileSize(), 0, 0, FilenameUtils.getName(fileName), Utils.toShortName(fileName));
	}

	ByHandleFileInformation toByHandleFileInformation() {
		return new ByHandleFileInformation(fileAttribute, creationTime, lastAccessTime, lastWriteTime,
				VirtualMount_Dokan.volumeSerialNumber, getFileSize(), 1, fileIndex);
	}

	int getFileSize() {
		return this.fileSize;
	}
	
	void setFileSize(int fileSize) {
		this.fileSize = fileSize;
	}

	static long getNextFileIndex() {
		return nextFileIndex++;
	}

//	public void setFileAttribute(int fileAttributes) {
//		this.fileAttribute = fileAttributes;
//	}
	public int getFileAttribute() {
		return fileAttribute;
	}
	long getCreationTime() {
		return creationTime;
	}
	void setCreationTime(long creationTime) {
		this.creationTime = creationTime;
	}
	public long getFileIndex() {
		return fileIndex;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public String getFileName() {
		return fileName;
	}
	void setLastAccessTime(long lastAccessTime) {
		this.lastAccessTime = lastAccessTime;
	}
	long getLastAccessTime() {
		return lastAccessTime;
	}
	void setLastWriteTime(long lastWriteTime) {
		this.lastWriteTime = lastWriteTime;
	}
	long getLastWriteTime() {
		return lastWriteTime;
	}
	public boolean isDirectory() {
		return isDirectory;
	}

//	@Override
//	@Iri("http://logicfishsoftware.com/wolfgo/filesys.owl#fileParent")
	public
	VirtualDirectory getParent() {
		return parent;
	}

//	@Override
//	@Iri("http://logicfishsoftware.com/wolfgo/filesys.owl#fileParent")
	public
	void setParent(VirtualDirectory fileParent) {
		this.parent = fileParent;
	}

//	@Override
//	@Iri("http://logicfishsoftware.com/wolfgo/filesys.owl#fileIndex")
//	public
//	void setFileIndex(long fileIndex) {
//		this.fileIndex = fileIndex;
//	}
}
